package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.Star;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface StarMapper {
    @Delete({
        "delete from star",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into star ( user_id, ",
        "target_id, target_type, ",
        "is_stared)",
        "values (#{userId,jdbcType=INTEGER}, ",
        "#{targetId,jdbcType=INTEGER}, #{targetType,jdbcType=VARCHAR}, ",
        "#{isStared,jdbcType=BIT})"
    })
    int insert(Star record);

    @Select({
        "select",
        "id, user_id, target_id, target_type, is_stared",
        "from star",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results(id = "ResultMap",value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="target_id", property="targetId", jdbcType=JdbcType.INTEGER),
        @Result(column="target_type", property="targetType", jdbcType=JdbcType.VARCHAR),
        @Result(column="is_stared", property="isStared", jdbcType=JdbcType.BIT)
    })
    Star selectByPrimaryKey(Integer id);

    //查询当前帖子(其它用户)的收藏(关注)数量(加上.size())
    @SelectProvider(type = QueryBuilder.class,method = "starQueryByTargetId")
    @ResultMap("ResultMap")
    List<Star> starQueryByTargetId(@Param("targetId")int targetId,@Param("targetType")String targetType);

    //查看当前用户所有收藏(关注)的帖子(其它用户)的数量(加上.size())
    @SelectProvider(type = QueryBuilder.class,method = "starQueryByUserId")
    @ResultMap("ResultMap")
    List<Star> starQueryByUserId(@Param("userId")int userId,@Param("targetType")String targetType);

    //查看当前用户对当前帖子的收藏状态(加上.getIsStared())
    @SelectProvider(type = QueryBuilder.class,method = "queryMyselfStar")
    @ResultMap("ResultMap")
    Star queryMyselfStar(@Param("targetId")int targetId,@Param("targetType")String targetType,@Param("userId")int userId);

    @UpdateProvider(type = UpdateBuilder.class,method = "updateStarStatusByUserIdAndTargetId")
    int updateStarStatusByUserIdAndTargetId(@Param("targetId")int targetId,@Param("targetType")String targetType,@Param("status")boolean status,@Param("userId")int userId);

    @Select({
        "select",
        "id, user_id, target_id, target_type, is_stared",
        "from star"
    })
    @ResultMap("ResultMap")

    List<Star> selectAll();

    @Update({
        "update star",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "target_id = #{targetId,jdbcType=INTEGER},",
          "target_type = #{targetType,jdbcType=VARCHAR},",
          "is_stared = #{isStared,jdbcType=BIT}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Star record);
}